home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1423 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  56 lines

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Location of an array?
  5. Date: 13 Jan 1996 15:08:33 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4d93g1$bj1@umbc9.umbc.edu>
  8. References: <4d4iqk$hs3@overload.lbl.gov>
  9. NNTP-Posting-Host: f-umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Mikhail Faiguenblat  <mfaiguen> wrote:
  13. |> I am sure this has been answered a million times by now, but could someody
  14. |> help me, please?
  15. |> 
  16. |> I have an array in my program, and I need to find out the absolute location
  17. |> in memory where this array is located. 
  18. |>
  19. |> I have tried to do it this way:
  20. |> 
  21. |> #include <stdio.h>
  22. |> 
  23. |> int main() {
  24. |>     char arr[10];
  25. |>     int addr;
  26. |> 
  27. |>     printf("sizeof(char *) = %d\n", sizeof(char *));
  28. |>     printf("sizeof(int) = %d\n", sizeof(int));
  29. |> 
  30. |>     addr = arr;
  31.  
  32. Just because the size is the same doesn't make this assignment legal. My
  33. compiler refuses to even compile this unless I put an explicit cast.
  34.  
  35. |>     printf("addr = %d\n", addr);
  36.  
  37. Only 2 types of pointers can be passed to printf(). A char * as in a
  38. NUL terminated string and a void *. Now with the void * it's hard to
  39. say what you will get, but probably some hex value that is not necessarily
  40. the true memory address. I'd suggest doing:
  41.  
  42. printf ("addr = %p\n", (void *) &arr[0]);
  43.  
  44. At least that's legal C.
  45.  
  46. |>     return 0;
  47. |> }
  48. |> 
  49. |> And it did not work:
  50.  
  51. <rest deleted>
  52. -- 
  53. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  54.  
  55. Jonas J. Schlein  (schlein@gl.umbc.edu)
  56.